home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /***************************************************************************
- * @(#) - BZWHO - Multiplayer tank game inquirer.
- *
- * $Id: bzwho.c,v 1.4 1993/08/11 19:46:47 adele Exp $
- *
- * Chris Fouts - Silicon Graphics, Inc.
- * October, 1991
- **************************************************************************/
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <bstring.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <arpa/inet.h>
-
-
- #include "bz.h"
- #include "multicast.h"
-
- /* BEGIN PROTOTYPES -S bzwho.c */
- /* END PROTOTYPES -S bzwho.c */
-
- static char *version_id = "$Id: bzwho.c,v 1.4 1993/08/11 19:46:47 adele Exp $" ;
-
-
- struct BzMember player[MAXPLAYERS] ;
- unsigned short key[MAXPLAYERS] ;
- static unsigned number_players ;
- static int in_fd ;
- static int in_addrlen ;
- static struct sockaddr_in in_addr ;
- static char *teamName[] = { "Green", "Red", "Blue" } ;
-
-
- main(
- int argc,
- char **argv
- )
- {
- int i ;
- int j = 0 ;
- int k ;
- int id ;
- struct in_addr addr ;
- struct hostent *hp ;
-
- number_players = 1 ;
-
- init_com() ;
-
- for( i = 0 ; i < 3 ; i++ ) {
- sleep( 1 ) ;
- network_in() ;
- }
-
- if( number_players > 1 ) {
-
- for( i = 1 ; i < number_players ; i++ ) {
- for( k = 0 ; k < j ; k++ ) {
- if( player[i].key == key[k] )
- break ;
- }
- if( k == j ) {
- key[j++] = player[i].key ;
- }
- }
-
- printf("\n\n BZ Scores\n\n");
- id = ( BZ & 0x00ff ) + 15 ;
- #if defined(BETA)
- printf( " Beta Version %d.%d", id / 10, id % 10 ) ;
- #else
- printf( " Version %d.%d", id / 10, id % 10 ) ;
- #endif /* defined(BETA) */
-
- for( k = 0 ; k < j ; k++ ) {
- printf( "\n\nGame %d - ", k ) ;
- if( key[k] == 0 ) {
- printf( "(no key)" ) ;
- }
- else {
- if( key[k] & PRIVATE_GAME ) {
- printf( "(private game) " ) ;
- }
- else if ( key[k] & GAME_KEY_MASK ) {
- printf( "(public key = %hd) ", key[k] & GAME_KEY_MASK ) ;
- }
- if( key[k] & NO_FLAG_GAME ) {
- printf( "(no flags)" ) ;
- }
- if( key[k] & FAST_GAME ) {
- printf( "(fast game)" ) ;
- }
- }
- printf( ":\n\n" ) ;
-
- for( i = 1 ; i < number_players ; i++ ) {
- if( player[i].key == key[k] ) {
- addr.s_addr = player[i].id ;
- hp = gethostbyaddr( &addr, sizeof(addr), AF_INET ) ;
- printf( "%-10s %6d %8s (%s)\n", player[i].name,
- player[i].score, teamName[player[i].team],
- hp ? hp->h_name : inet_ntoa(addr) ) ;
- }
- }
- }
- }
- else {
- printf("\n\nNo one is playing now.\n\n");
- }
- printf("\n");
-
- return( 0 ) ;
- }
-
-
-
- int find_player(
- long id
- )
- {
- int i ;
- int found ;
-
- found = 0 ;
- i = 1 ;
- while( i < number_players && !found ) {
- if( player[i].id == id ) found = i ;
- i++ ;
- }
-
- return(found);
- }
-
-
- void add_player( int *number )
- {
- if( number_players <= MAXPLAYERS ) {
- *number = number_players ;
- number_players++ ;
- }
- else {
- printf( "Maximum number (%d) of players exceeded.\n", MAXPLAYERS ) ;
- exit( 0 ) ;
- }
- }
-
-
-
- void copy_player(
- int k,
- struct BzMember *psrc
- )
- {
- bcopy( psrc, &(player[k]), sizeof( struct BzMember ) ) ;
- }
-
-
-
- void process_packet(
- struct BzMember *input
- )
- {
- int number ;
- static int newer = 0 ;
-
- if( input->bz_id == BZ ) {
- /* either update existing player or create a new one */
- if( !( number = find_player( input->id ) ) ) {
- add_player( &number ) ;
- copy_player( number, input ) ;
- }
- }
- else if( input->bz_id == BZMSG ) {
- }
- else if( input->bz_id > BZ &&
- ( input->bz_id & 0xffffff00 ) == (BZ & 0xffffff00) &&
- newer == 0 ) {
- newer = 1 ;
- printf("\n\nYou are using an older version of bzwho.\n\n");
- }
- }
-
-
-
- void network_in( void )
- {
- int no_chars_received ;
- struct BzMember input ;
- int socket_empty = 0 ;
- extern int errno ;
-
- while( !socket_empty ) {
- no_chars_received = recvfrom( in_fd, (char *)(&input),
- sizeof(struct BzMember), 0, &in_addr,
- &in_addrlen ) ;
- if ( no_chars_received == sizeof(struct BzMember) ) {
- process_packet( &input ) ;
- }
- else if( no_chars_received < 0 && errno == EWOULDBLOCK )
- socket_empty = 1 ;
- }
- }
-
-
-
- static char *service_msg[] = {
- "Can't find udp service \"sgi-bznet\"\n\n",
- "To run bz over the network, you must have the following line\n",
- "in your /etc/services file.\n\n",
- "sgi-bz\t5133/udp\t\t# tank demo\n\n",
- "(Using port number 5133 by default.)\n\n"
- } ;
-
-
- void init_com( void )
- {
- int i ;
- int port ;
- struct servent *serv ;
-
- if( ( serv = getservbyname( BZ_SERVICE, NULL ) ) == NULL ) {
- for( i = 0 ; i < sizeof( service_msg ) / sizeof( service_msg[0] ) ;
- i++ ) {
- fputs( service_msg[i], stderr ) ;
- }
- port = 5133 ;
- }
- else {
- port = serv->s_port ;
- }
- #if defined( BETA )
- port = 5134 ;
- #endif /* defined( BETA ) */
- if( ( in_fd = openMulticastSocket( &in_addr, port, BZ_DEFAULT_TTL, 0,
- BZ_GROUP, NULL, "r" ) ) < 0 ) {
- exit( 1 ) ;
- }
-
- /*
- * Turn on non-blocking I/O
- */
- if( fcntl( in_fd, F_SETFL, FNDELAY ) < 0 ) {
- perror( "fcntl F_SETFL, FNDELAY" ) ;
- exit( 1 ) ;
- }
- }
-
-
-
-